home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / mesa / mesa-tk / samples.tk / bitmap1.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  6KB  |  264 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include "gltk.h"
  29.  
  30. #define OPENGL_WIDTH 24
  31. #define OPENGL_HEIGHT 13
  32.  
  33. GLenum rgb, doubleBuffer, directRender, windType;
  34.  
  35. float boxA[3] =
  36. {
  37.   0, 0, 0
  38. };
  39. float boxB[3] =
  40. {
  41.   -100, 0, 0
  42. };
  43. float boxC[3] =
  44. {
  45.   100, 0, 0
  46. };
  47. float boxD[3] =
  48. {
  49.   0, 95, 0
  50. };
  51. float boxE[3] =
  52. {
  53.   0, -105, 0
  54. };
  55. GLubyte OpenGL_bits1[] =
  56. {
  57.   0x00, 0x03, 0x00,
  58.   0x7f, 0xfb, 0xff,
  59.   0x7f, 0xfb, 0xff,
  60.   0x00, 0x03, 0x00,
  61.   0x3e, 0x8f, 0xb7,
  62.   0x63, 0xdb, 0xb0,
  63.   0x63, 0xdb, 0xb7,
  64.   0x63, 0xdb, 0xb6,
  65.   0x63, 0x8f, 0xf3,
  66.   0x63, 0x00, 0x00,
  67.   0x63, 0x00, 0x00,
  68.   0x63, 0x00, 0x00,
  69.   0x3e, 0x00, 0x00,
  70. };
  71. GLubyte OpenGL_bits2[] =
  72. {
  73.   0x00, 0x00, 0x00,
  74.   0xff, 0xff, 0x01,
  75.   0xff, 0xff, 0x01,
  76.   0x00, 0x00, 0x00,
  77.   0xf9, 0xfc, 0x01,
  78.   0x8d, 0x0d, 0x00,
  79.   0x8d, 0x0d, 0x00,
  80.   0x8d, 0x0d, 0x00,
  81.   0xcc, 0x0d, 0x00,
  82.   0x0c, 0x4c, 0x0a,
  83.   0x0c, 0x4c, 0x0e,
  84.   0x8c, 0xed, 0x0e,
  85.   0xf8, 0x0c, 0x00,
  86. };
  87. GLubyte logo_bits[] =
  88. {
  89.   0x00, 0x66, 0x66,
  90.   0xff, 0x66, 0x66,
  91.   0x00, 0x00, 0x00,
  92.   0xff, 0x3c, 0x3c,
  93.   0x00, 0x42, 0x40,
  94.   0xff, 0x42, 0x40,
  95.   0x00, 0x41, 0x40,
  96.   0xff, 0x21, 0x20,
  97.   0x00, 0x2f, 0x20,
  98.   0xff, 0x20, 0x20,
  99.   0x00, 0x10, 0x90,
  100.   0xff, 0x10, 0x90,
  101.   0x00, 0x0f, 0x10,
  102.   0xff, 0x00, 0x00,
  103.   0x00, 0x66, 0x66,
  104.   0xff, 0x66, 0x66,
  105. };
  106.  
  107. static void Init(void)
  108. {
  109.  
  110.   glClearColor(0.0, 0.0, 0.0, 0.0);
  111.   glClearIndex(0.0);
  112. }
  113.  
  114. static void Reshape(int width, int height)
  115. {
  116.  
  117.   glViewport(0, 0, (GLint) width, (GLint) height);
  118.  
  119.   glMatrixMode(GL_PROJECTION);
  120.   glLoadIdentity();
  121.   gluOrtho2D(-175, 175, -175, 175);
  122.   glMatrixMode(GL_MODELVIEW);
  123. }
  124.  
  125. static GLenum Key(int key, GLenum mask)
  126. {
  127.  
  128.   switch (key) {
  129.     case TK_ESCAPE:
  130.       tkQuit();
  131.   }
  132.   return GL_FALSE;
  133. }
  134.  
  135. static void Draw(void)
  136. {
  137.   float mapI[2], mapIA[2], mapIR[2];
  138.  
  139.   glClear(GL_COLOR_BUFFER_BIT);
  140.  
  141.   mapI[0] = 0.0;
  142.   mapI[1] = 1.0;
  143.   mapIR[0] = 0.0;
  144.   mapIR[1] = 0.0;
  145.   mapIA[0] = 1.0;
  146.   mapIA[1] = 1.0;
  147.  
  148.   glPixelMapfv(GL_PIXEL_MAP_I_TO_R, 2, mapIR);
  149.   glPixelMapfv(GL_PIXEL_MAP_I_TO_G, 2, mapI);
  150.   glPixelMapfv(GL_PIXEL_MAP_I_TO_B, 2, mapI);
  151.   glPixelMapfv(GL_PIXEL_MAP_I_TO_A, 2, mapIA);
  152.   glPixelTransferi(GL_MAP_COLOR, GL_TRUE);
  153.  
  154.   glRasterPos3fv(boxA);
  155.   glPixelStorei(GL_UNPACK_ROW_LENGTH, 24);
  156.   glPixelStorei(GL_UNPACK_SKIP_PIXELS, 8);
  157.   glPixelStorei(GL_UNPACK_SKIP_ROWS, 2);
  158.   glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
  159.   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  160.   glBitmap(16, 12, 8.0, 0.0, 0.0, 0.0, logo_bits);
  161.  
  162.   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  163.   glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  164.   glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  165.   glPixelStorei(GL_UNPACK_LSB_FIRST, GL_TRUE);
  166.   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  167.  
  168.   TK_SETCOLOR(windType, TK_WHITE);
  169.   glRasterPos3fv(boxB);
  170.   glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
  171.        OpenGL_bits1);
  172.   glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
  173.        OpenGL_bits2);
  174.  
  175.   TK_SETCOLOR(windType, TK_YELLOW);
  176.   glRasterPos3fv(boxC);
  177.   glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
  178.        OpenGL_bits1);
  179.   glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
  180.        OpenGL_bits2);
  181.  
  182.   TK_SETCOLOR(windType, TK_CYAN);
  183.   glRasterPos3fv(boxD);
  184.   glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
  185.        OpenGL_bits1);
  186.   glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
  187.        OpenGL_bits2);
  188.  
  189.   TK_SETCOLOR(windType, TK_RED);
  190.   glRasterPos3fv(boxE);
  191.   glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
  192.        OpenGL_bits1);
  193.   glBitmap(OPENGL_WIDTH, OPENGL_HEIGHT, OPENGL_WIDTH, 0.0, OPENGL_WIDTH, 0.0,
  194.        OpenGL_bits2);
  195.  
  196.   glFlush();
  197.  
  198.   if (doubleBuffer) {
  199.     tkSwapBuffers();
  200.   }
  201. }
  202.  
  203. static GLenum Args(int argc, char **argv)
  204. {
  205.   GLint i;
  206.  
  207.   rgb = GL_TRUE;
  208.   doubleBuffer = GL_FALSE;
  209.   directRender = GL_TRUE;
  210.  
  211.   for (i = 1; i < argc; i++) {
  212.     if (strcmp(argv[i], "-ci") == 0) {
  213.       rgb = GL_FALSE;
  214.     }
  215.     else if (strcmp(argv[i], "-rgb") == 0) {
  216.       rgb = GL_TRUE;
  217.     }
  218.     else if (strcmp(argv[i], "-sb") == 0) {
  219.       doubleBuffer = GL_FALSE;
  220.     }
  221.     else if (strcmp(argv[i], "-db") == 0) {
  222.       doubleBuffer = GL_TRUE;
  223.     }
  224.     else if (strcmp(argv[i], "-dr") == 0) {
  225.       directRender = GL_TRUE;
  226.     }
  227.     else if (strcmp(argv[i], "-ir") == 0) {
  228.       directRender = GL_FALSE;
  229.     }
  230.     else {
  231.       printf("%s (Bad option).\n", argv[i]);
  232.       return GL_FALSE;
  233.     }
  234.   }
  235.   return GL_TRUE;
  236. }
  237.  
  238. void main(int argc, char **argv)
  239. {
  240.  
  241.   if (Args(argc, argv) == GL_FALSE) {
  242.     tkQuit();
  243.   }
  244.  
  245.   tkInitPosition(0, 0, 300, 300);
  246.  
  247.   windType = (rgb) ? TK_RGB : TK_INDEX;
  248.   windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  249.   windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  250.   tkInitDisplayMode(windType);
  251.  
  252.   if (tkInitWindow("Bitmap Test") == GL_FALSE) {
  253.     tkQuit();
  254.   }
  255.  
  256.   Init();
  257.  
  258.   tkExposeFunc(Reshape);
  259.   tkReshapeFunc(Reshape);
  260.   tkKeyDownFunc(Key);
  261.   tkDisplayFunc(Draw);
  262.   tkExec();
  263. }
  264.